var happyReaderMenu = {
	selection:'',
	getSelectionHTML:function(){
        var userSelection = window.getSelection();
	    if (userSelection.isCollapsed) {
			return '';
	    }
	    
	    var range = userSelection.getRangeAt(0),
	        clonedSelection = range.cloneContents();
	    
	    var div = document.createElement('div');
	    div.appendChild(clonedSelection);
	    
	    //convert relative address to absolute
	    var hrefs = div.querySelectorAll('[href]');
	    for (var i = 0, len = hrefs.length; i < len; i++) 
	        hrefs[i].href = hrefs[i].href;
	    var srcs = div.querySelectorAll('[src]');
	    for (var i = 0, len = srcs.length; i < len; i++) 
	        srcs[i].src = srcs[i].src;
	    
	    return div.innerHTML;
	}
};
document.addEventListener(
    'mouseup', 
    function(e) {
		var str = happyReaderMenu.selection = happyReaderMenu.getSelectionHTML();
		chrome.extension.connect({name:'rightMenuConn'})
		.postMessage({action:'updateRightMenu', selection:str});
    }, 
    false
);
